package com.jiyun.controller;
/*
 * @Author:limengyao
 * @Date:2023-08-22 15:17
 * @Filename:UserController
 *
 */

import com.alibaba.fastjson.JSON;
import com.jiyun.ResultClasss;
import com.jiyun.pojo.day01.Member;
import com.jiyun.pojo.day02.ResultClass;
import com.jiyun.pojo.day02.User;
import com.jiyun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/userlogin")
    public ResultClasss userlogin(@RequestBody Member member){
        try {
            ResultClasss re=userService.userlogin(member);
            return ResultClasss.ok(re);
        }catch (Exception e){
            e.printStackTrace();
            return ResultClasss.error();
        }
    }
    @GetMapping("/findLoginUser")
    public ResultClasss findLoginUser(HttpServletRequest request){
        String token = request.getHeader("token");
        Member user = JSON.parseObject((String) redisTemplate.opsForHash().get("用户信息", token), Member.class);
            return ResultClasss.ok(ResultClass.ok(user));
    }
}
